package com.tourism.home.controller.common;

import com.tourism.home.entity.Result;
import com.tourism.home.service.UploadService;
import com.tourism.home.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class UploadController {

    @Autowired
    HttpServletRequest httpServletRequest;

    @Autowired
    UploadService uploadService;

    @RequestMapping("/upload/icon")
    Result uploadIcon(@RequestParam("file") MultipartFile file){
        HttpSession httpSession = httpServletRequest.getSession();
        if(uploadService.uploadIcon(file)){
            return ResultUtil.success(httpSession.getAttribute("icon"));
        }else {
            return ResultUtil.error(null);
        }
    }

    @RequestMapping("/upload/postimage")
    Result uploadImage(@RequestParam("file") MultipartFile file){
        HttpSession httpSession = httpServletRequest.getSession();
        if(uploadService.uploadimage(file)){
            return ResultUtil.success(httpSession.getAttribute("data"));
        }else {
            return ResultUtil.error(null);
        }
    }

}
